﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.Console;
using NLog.Extensions.Logging;

namespace _03_Logging
{
    /// <summary>
    /// https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/logging/?view=aspnetcore-7.0
    /// </summary>
    internal class Program
    {
        static void Main(string[] args)
        {
            Test5();
        }

        public static void Test1()
        {
            //创建日志工厂
            ILoggerFactory factory = LoggerFactory.Create(configure =>
            {
                //需要Microsoft.Extensions.Logging.Console的支持
                configure.AddConsole(c =>
                {
                    c.FormatterName = ConsoleFormatterNames.Simple;
                });
                //需要Microsoft.Extensions.Logging.Debug的支持
                configure.AddDebug();
                //自定义扩展
                configure.AddFile();
                configure.SetMinimumLevel(LogLevel.Information);
            });
            //获取一个名为Program的日志实列
            ILogger<Program> logger1 = factory.CreateLogger<Program>();
            //有时候无法使用泛型的使用可以使用这个api
            //注意：CreateLogger返回的是微软的一个实现，里面做了大量的封装，用于简化你的封装成本。
            ILogger logger2 = factory.CreateLogger(nameof(Program));
            logger1.LogInformation("Hello World!");
            logger1.LogWarning("Warning Hello World!");
            using (logger2.BeginScope(0))
            {
                logger2.LogInformation("scope...");
            }
            Console.ReadKey();
        }
        /// <summary>
        /// Filter
        /// </summary>
        public static void Test2()
        {
            ILoggerFactory factory = LoggerFactory.Create(configure =>
            {
                configure.AddConsole(c =>
                {
                    c.FormatterName = ConsoleFormatterNames.Simple;
                });
                configure.AddFile();
                //作用于所有日志提供程序
                configure.AddFilter((categorName, level) =>
                {
                    if (categorName.StartsWith("Microsoft.EntityFramework"))
                    {
                        return true;
                    }
                    return false;
                });
                //只作用与指定日志提供程序
                //configure.AddFilter<ConsoleLoggerProvider>((categorName, level) =>
                //{
                //    if (categorName.StartsWith("Microsoft.EntityFramework"))
                //    {
                //        return true;
                //    }
                //    return false;
                //});
            });
            var logger1 = factory.CreateLogger("Microsoft.EntityFramework");
            var logger2 = factory.CreateLogger("Microsoft.AspNetCore");
            //推荐使用占位符，而不是字符串拼接，因为字符串拼接即使日志不打印，也要拼接，调用toString性能不行
            logger1.LogInformation("fff{0}", DateTime.Now);
            logger2.LogInformation("fff", DateTime.Now);
            using (logger1.BeginScope(0))
            {
                logger1.LogInformation("12313");
            }
        }
        /// <summary>
        /// Configuration
        /// </summary>
        public static void Test3()
        {
            //创建配置
            var configuration = new ConfigurationManager();
            configuration.SetBasePath(Directory.GetCurrentDirectory());
            configuration.AddJsonFile("config.json");
            ILoggerFactory factory = LoggerFactory.Create(configure =>
            {
                //使用配置文件
                configure.AddConfiguration(configuration.GetSection("Logging"));
                configure.AddConsole(c =>
                {
                    c.FormatterName = ConsoleFormatterNames.Simple;
                });
                configure.AddFile();
            });

            factory.CreateLogger("Microsoft.EntityFramework").LogInformation("ef core..");
            factory.CreateLogger("Microsoft.AspNetCore").LogInformation("aspnet core..");
            factory.CreateLogger("HuaJianDao.Logging").LogInformation("logging..");
        }
        /// <summary>
        /// NLog
        /// </summary>
        public static void Test4()
        {
            //https://nlog-project.org/config
            ILoggerFactory factory = LoggerFactory.Create(configure =>
            {
                //使用配置文件
                configure.AddNLog();
            });
            var logger1 = factory.CreateLogger<ILogger<Program>>();
            var logger2 = factory.CreateLogger("Program");
            logger1.LogInformation("uuuuuuuuuu");
            logger2.LogInformation("uuuuuuuuuu");
        }
        /// <summary>
        /// IOC集成
        /// </summary>
        public static void Test5()
        {
            var services = new ServiceCollection();
            services.AddLogging(c =>
            {
                c.AddConsole();
            });
            Console.WriteLine("打印注册的服务");
           
            var sp = services.BuildServiceProvider();
            var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
            var logger = sp.GetRequiredService<ILogger<Program>>();
        }
    }
}